// packageX/business_district_management/member_title/member_title.js
var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    titles: [
    ],
    showPopup: false,
    popupType: 'add', // 'add' 或 'edit'
    currentTitleId: '',
    formData: {
      name: '',
      description: '',
      sort: ''
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    // 可以从options中获取districtId
    if (options && options.id) {
      this.setData({
        districtId: options.id
      });
    }
    // 实际项目中应该调用API获取头衔列表
    this.getTitles();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.getTitles();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 获取头衔列表
   */
  getTitles() {
    let that = this;
    let urlStr = app.getNetAddresss("plugin.commerce.api.commerce-member.get-categories");
    app._getNetWork({
      url: urlStr,
      data: {
        circle_id: this.data.districtId
      },
      success: function(resdata) {
        console.log(resdata);
        var res = resdata.data;
        if (res.result == 1) {
          that.setData({
            titles: res.data.list || []
          });
        } else {
          wx.showToast({
            icon: 'none',
            title: res.msg,
            duration: 1500
          });
        }
      },
      fail: function(res) {
        console.log(res);
      }
    });
  },

  /**
   * 添加头衔点击事件
   */
  onAddTitleTap() {
    this.setData({
      popupType: 'add',
      currentTitleId: '',
      formData: {
        name: '',
        description: '',
        sort: ''
      },
      showPopup: true
    });
  },

  /**
   * 编辑头衔点击事件
   */
  onEditTitleTap(e) {
    const id = e.currentTarget.dataset.id;
    const title = this.data.titles.find(item => item.id === id);
    if (title) {
      this.setData({
        popupType: 'edit',
        currentTitleId: id,
        formData: {
          name: title.category || title.name,
          description: title.description || '',
          sort: title.sort || ''
        },
        showPopup: true
      });
    }
  },

  /**
   * 删除头衔点击事件
   */
  onDeleteTitleTap(e) {
    const id = e.currentTarget.dataset.id;
    wx.showModal({
      title: '确认删除',
      content: '确定要删除这个头衔吗？',
      success: (res) => {
        if (res.confirm) {
          let that = this;
          let urlStr = app.getNetAddresss("plugin.commerce.api.commerce-member.delete-category");
          app._getNetWork({
            url: urlStr,
            data: {
              id: id
            },
            success: function(resdata) {
              console.log(resdata);
              var res = resdata.data;
              if (res.result == 1) {
                wx.showToast({
                  title: '删除成功',
                  icon: 'success'
                });
                // 删除成功后重新获取头衔列表
                that.getTitles();
              } else {
                wx.showToast({
                  icon: 'none',
                  title: res.msg,
                  duration: 1500
                });
              }
            },
            fail: function(res) {
              console.log(res);
              wx.showToast({
                icon: 'none',
                title: '删除失败',
                duration: 1500
              });
            }
          });
        }
      }
    });
  },

  /**
   * 输入框内容变化
   */
  onInputChange(e) {
    const field = e.currentTarget.dataset.field;
    const value = e.detail.value;
    this.setData({
      [`formData.${field}`]: value
    });
  },

  /**
   * 关闭弹窗
   */
  onClosePopup() {
    this.setData({
      showPopup: false
    });
  },

  /**
   * 确认弹窗
   */
  onConfirmPopup() {
    const { name, description } = this.data.formData;
    
    if (!name.trim()) {
      wx.showToast({
        title: '请输入头衔名称',
        icon: 'none'
      });
      return;
    }
    
    if (this.data.popupType === 'add') {
      // 调用API添加头衔
      let that = this;
      let urlStr = app.getNetAddresss("plugin.commerce.api.commerce-member.add-category");
      app._getNetWork({
        url: urlStr,
        data: {
          circle_id: this.data.districtId,
          category: name,
          sort: parseInt(this.data.formData.sort) || 0
        },
        success: function(resdata) {
          console.log(resdata);
          var res = resdata.data;
          if (res.result == 1) {
            that.getTitles();
            that.onClosePopup();
            wx.showToast({
              title: '添加成功',
              icon: 'success'
            });
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
      
    } else {
      // 调用API编辑头衔
      let that = this;
      let urlStr = app.getNetAddresss("plugin.commerce.api.commerce-member.edit-category");
      app._getNetWork({
        url: urlStr,
        data: {
          id: this.data.currentTitleId,
          category: name,
          sort: parseInt(this.data.formData.sort) || 0
        },
        success: function(resdata) {
          console.log(resdata);
          var res = resdata.data;
          if (res.result == 1) {
            that.getTitles();
            that.onClosePopup();
            wx.showToast({
              title: '编辑成功',
              icon: 'success'
            });
          } else {
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    }
  }
});